package com.gzsxy.sso.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzsxy.sso.common.exception.impl.BusinessException;
import com.gzsxy.sso.core.model.CustomUser;
import com.gzsxy.sso.core.model.SessionUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import springfox.documentation.spring.web.json.Json;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 会话用户工具类
 * @date 2021/11/9 17:55
 */
public class SessionUserUtil {



    /**
     * @description: 获取当前会话用户
     * @param
     * @return SessionUser
     */
    public static SessionUser getSessionUser(){
        try {
            //获取当前认证权限上下文
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            OAuth2Authentication auth2Authentication = (OAuth2Authentication) auth;
            SessionUser sessionUser = null;
            //获取认证用户信息
            if (auth2Authentication.getPrincipal() != null){
                Class<?> typeClass = auth2Authentication.getPrincipal().getClass();
                //判断用户类型是否为CustomUser
                if (typeClass.equals(CustomUser.class)){
                    CustomUser customUser = (CustomUser)auth2Authentication.getPrincipal();
                    //会话用户
                    sessionUser = new SessionUser();
                    BeanUtils.copyProperties(customUser,sessionUser);
                } else {
                    //getDetails()获取的是密码模式的四个参数值
                    Map<String,Object> details = (Map<String,Object>)auth2Authentication.getUserAuthentication().getDetails();
                    //用户名密码模式登录
                    if (details != null && details.containsKey("id")){
                        String userPrincipal = JSONObject.toJSONString(details);
                        sessionUser = JSON.parseObject(userPrincipal, SessionUser.class);
                    }
                }
            }
            //用户id不为空
            if (null != sessionUser && StringUtils.isNotEmpty(sessionUser.getId())){
                return sessionUser;
            }
        } catch (Exception e) {
            throw  BusinessException.getInstance("会话用户丢失");
        }
        throw  BusinessException.getInstance("会话用户丢失");
    }




    /**
     * @description: 构建token附加信息
     * @param auth2Authentication  认证信息
     * @return
     */
    public static Map<String ,Object> buildTokenAdditionalInformation(OAuth2Authentication auth2Authentication){
        Map<String, Object> additionalInformation = new LinkedHashMap<>();

        try {
            //获取认证用户信息
            CustomUser customUser = (CustomUser)auth2Authentication.getPrincipal();
            //会话用户
            SessionUser sessionUser = new SessionUser();
            BeanUtils.copyProperties(customUser,sessionUser);


            //用户id不为空
            if (null != sessionUser && StringUtils.isNotEmpty(sessionUser.getId())){
                //转为map对象
                Map<String,Object> sessionUserMap = JSON.parseObject(sessionUser.toString(),Map.class);
                additionalInformation.putAll(sessionUserMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return additionalInformation;
    }



    /**
     * @description: 判断当前登录用户是否管理员或者超级管理员
     */
    public static boolean isSAdmin(){
        SessionUser sessionUser = getSessionUser();
        return sessionUser.getIsSadmin();

    }



    /**
     * @description: 判断当前登录用户是否管理员或者超级管理员
     */
    public static boolean isAdmin(){
        SessionUser sessionUser = getSessionUser();
        return sessionUser.getIsSadmin() || sessionUser.getIsSadmin();

    }
}
